package com.crazyktv.wcf.android;

import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.crazyktv.wcf.android.Util.DoAction;

@TargetApi(9)
public class Numpad extends Activity {
	private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0,
			btnInsert, btnOrder;
	private ImageButton btnBack;
	private EditText orderNum;
	protected static Vibrator myVibrator = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_numpad);
		findViews();
		myVibrator = (Vibrator) getApplication().getSystemService(
				Service.VIBRATOR_SERVICE);
	}

	private void findViews() {
		orderNum = (EditText) this.findViewById(R.id.EditText_order_num);
		orderNum.addTextChangedListener(new TextWatcher(){
			public void afterTextChanged(Editable s) {
				boolean numlimit;
				if (Util.prefNumLimit) {
					numlimit = (orderNum.length() == Util.prefNumLen) ? true: false;
				} else {
					
					numlimit = (orderNum.length() > 3)?true:false;
				}
				if (numlimit) {
					try {
						JSONArray jsona = new JSONArray(Util.QuerySong(
								"Song_Id%3D"
										+ orderNum.getText().toString(), 1));
						JSONObject json = new JSONObject(jsona.getString(0));
						String t = json.getString("Song_Singer")+" - "+json.getString("Song_SongName");
						if (t.length() != 0) {
							Toast.makeText(Numpad.this, t,
									Toast.LENGTH_SHORT).show();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
	          }

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				
			}
		});
		btn0 = (Button) this.findViewById(R.id.button0);
		btn1 = (Button) this.findViewById(R.id.button1);
		btn2 = (Button) this.findViewById(R.id.button2);
		btn3 = (Button) this.findViewById(R.id.button3);
		btn4 = (Button) this.findViewById(R.id.button4);
		btn5 = (Button) this.findViewById(R.id.button5);
		btn6 = (Button) this.findViewById(R.id.button6);
		btn7 = (Button) this.findViewById(R.id.button7);
		btn8 = (Button) this.findViewById(R.id.button8);
		btn9 = (Button) this.findViewById(R.id.button9);
		btn1.setOnClickListener(numOCL(1));
		btn2.setOnClickListener(numOCL(2));
		btn3.setOnClickListener(numOCL(3));
		btn4.setOnClickListener(numOCL(4));
		btn5.setOnClickListener(numOCL(5));
		btn6.setOnClickListener(numOCL(6));
		btn7.setOnClickListener(numOCL(7));
		btn8.setOnClickListener(numOCL(8));
		btn9.setOnClickListener(numOCL(9));
		btn0.setOnClickListener(numOCL(0));
		btnInsert = (Button) this.findViewById(R.id.button_insert);
		btnOrder = (Button) this.findViewById(R.id.button_order);
		btnBack = (ImageButton) this.findViewById(R.id.button_back);

		btnBack.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (orderNum.getText().length() != 0) {
					orderNum.setText(orderNum.getText().subSequence(0,
							orderNum.length() - 1));
				}
			}
		});

		btnOrder.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				boolean send;
				if (Util.prefNumLimit) {
					send = (orderNum.length() == Util.prefNumLen) ? true
							: false;
				} else {
					send = (orderNum.length() > 0) ? true : false;
				}
				if (send) {
					try {
						JSONArray jsona = new JSONArray(Util.OrderSong(orderNum
								.getText().toString()));
						JSONObject json = new JSONObject(jsona.getString(0));
						String s = json.getString("Song_SongName");
						if (s.length()!=0) {
							Toast.makeText(
									Numpad.this,
									s
											+ " "
											+ getText(R.string.toast_order_success),
									Toast.LENGTH_SHORT).show();
							orderNum.setText("");
							myVibrator.vibrate(100);
						}
					} catch (Exception e) {
						e.printStackTrace();
						Toast.makeText(Numpad.this, R.string.toast_order_fail,
								Toast.LENGTH_SHORT).show();
					}

				}

			}
		});
		btnInsert.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				boolean send;
				if (Util.prefNumLimit) {
					send = (orderNum.length() == Util.prefNumLen) ? true
							: false;
				} else {
					send = (orderNum.length() > 0) ? true : false;
				}
				if (send) {
					String s = Util.DoAction(DoAction.INSERT, orderNum
							.getText().toString());
					if (s.length() < 5) {
						Toast.makeText(Numpad.this,
								getText(R.string.toast_insert_success),
								Toast.LENGTH_SHORT).show();
						orderNum.setText("");
						myVibrator.vibrate(100);
					} else {
						Log.d("insert", s);
						Toast.makeText(Numpad.this, R.string.toast_order_fail,
								Toast.LENGTH_SHORT).show();
					}

				}

			}
		});
	}

	private OnClickListener numOCL(final int num) {
		OnClickListener ocl = new View.OnClickListener() {
			public void onClick(View v) {
				boolean numlimit;

				if (Util.prefNumLimit) {
					numlimit = (orderNum.length() < Util.prefNumLen) ? true: false;
				} else {
					numlimit = true;
				}
				if (numlimit) {
					orderNum.append(String.valueOf(num));
				}
			}
		};
		return ocl;
	}

	// listen "back" action
	private long exitTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(getApplicationContext(), R.string.quit,
						Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				finish();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
